package com.example.hidesms.sqlite;

import java.sql.SQLException;

import com.example.hidesms.dao.BlockCallDAO;
import com.example.hidesms.dao.BlockSmsDAO;
import com.example.hidesms.dao.SmsDAO;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {

	private static final String TABLE_SMS = "privace_sms";
	private static final String TABLE_BLOCK_SMS = "block_sms";
	private static final String TABLE_BLOCK_CALL = "block_call";
	/** Global variables. */
	private static String DB_NAME = "PrivaceSms.db";
	private static int DATABASE_VERSION = 1;
	private SQLiteDatabase myDatabase;

	public DataBaseHelper(Context context) {
		super(context, DB_NAME, null, DATABASE_VERSION);
		try {
			openDataBase();
		} catch (SQLException e) {
		}
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(SmsDAO.CREATE_TABLE_SMS);
		db.execSQL(BlockSmsDAO.CREATE_TABLE_BLOCK_SMS);
		db.execSQL(BlockCallDAO.CREATE_TABLE_BLOCK_CALL);

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_SMS);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_BLOCK_SMS);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_BLOCK_CALL);
		onCreate(db);
	}

	@Override
	public synchronized void close() {
		if (myDatabase != null) {
			myDatabase.close();
		}
		super.close();
	}

	public void openDataBase() throws SQLException {
		myDatabase = getWritableDatabase();
	}

	public void closeDataBase() {
		if (myDatabase != null)
			myDatabase.close();
	}

	public void ExeSQLData(String sql) throws SQLException {
		myDatabase.execSQL(sql);
	}

	public void ExeSQLData(String sql, Object[] object) throws SQLException {
		myDatabase.execSQL(sql, object);
	}

	public Cursor QueryData(String query) throws SQLException {

		return myDatabase.rawQuery(query, null);
	}

	public Cursor QueryData(String query, String[] object) throws SQLException {
		return myDatabase.rawQuery(query, object);
	}
}